package com.example.change;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

	public static final String CASH_IS_EMPTY_KEY = "empty";
	public static final String HAS_CASH_KEY = "cash";
	public static final String TOO_MANY_DECIMAL_PLACES_KEY = "dp";
	// where you enter the cost
	private EditText enterCost;
	// where you enter the available cash
	private EditText enterFunds;
	// the submit button
	private Button submit;
	// the button that adds an item to the list
	private Button addCost;
	// the total cost of all items on the list
	private BigDecimal totalCost;
	// where the total cost is displayed
	private TextView textTotalCost;
	private List<Item> history;
	private String totalCostString; 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		getActionBar().hide();
		// setting the views
		enterCost = (EditText) findViewById(R.id.enterCost);
		enterFunds = (EditText) findViewById(R.id.enterFunds);
		textTotalCost = (TextView) findViewById(R.id.totalCost);
		submit = (Button) findViewById(R.id.submit);
		addCost = (Button) findViewById(R.id.addCost);
		// making totalcost equal zero to begin with
		totalCost = BigDecimal.ZERO;
		// makes this class the onclick listener for the button
		// meaning when a button is pressed the onclick method in this class is
		// used
		submit.setOnClickListener(this);
		addCost.setOnClickListener(this);
		
		// creating a list for history
		history = new ArrayList<Item>();
		
		if(savedInstanceState != null){
			//getting the saved state of the game in case of screen rotation 
			//or navigating away from the game
			totalCostString = savedInstanceState.getString("totalCostString");
			totalCost = new BigDecimal(totalCostString);
			totalCost = totalCost.setScale(2, RoundingMode.CEILING);
		}else{
			//the first instance of the game
			//getting the values from the intent in the start up activity
			totalCost = BigDecimal.ZERO;
			totalCost = totalCost.setScale(2, RoundingMode.CEILING);
		}
		textTotalCost.setText(totalCost.toString());
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		//saving the game data if screen is turned etc
		totalCostString = totalCost.toString();
		outState.putString("totalCostString", totalCostString);
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onClick(View view) {
		//makes sure keyboard is closed when anything is clicked
		InputMethodManager imm = (InputMethodManager)getSystemService(
			      Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(enterCost.getWindowToken(), 0);
			imm.hideSoftInputFromWindow(enterFunds.getWindowToken(), 0);
			
			String textCost = enterCost.getText().toString();
			String textCash = enterFunds.getText().toString();
			
		if (submit == view) {
			// gets the available cash and checks to see if there has been a
			// value entered
			// if no value has been entered it sends a dialog box warning the
			// user
			
			if (textCash.isEmpty() || totalCost.equals(BigDecimal.ZERO)) {
				
				MyDialogFragment myDialogFragment = new MyDialogFragment();
		        Bundle args = new Bundle();
		        args.putString(CASH_IS_EMPTY_KEY, "You have entered no money!");
		        myDialogFragment.setArguments(args);
		        myDialogFragment.show(getFragmentManager(), "tag");

			} if (new BigDecimal(textCash).scale()>2 ){
				MyDialogFragment myDialogFragment = new MyDialogFragment();
		        Bundle args = new Bundle();
		        args.putString(TOO_MANY_DECIMAL_PLACES_KEY, "You have entered too many decimal places!");
		        myDialogFragment.setArguments(args);
		        myDialogFragment.show(getFragmentManager(), "tag");
		        
			}else {
				// makes the cash into a big decimal
				// calculates the change needed by using the totalcost
				BigDecimal cash = new BigDecimal(textCash);
				// sets the result to a string
				String change;
				try {
					change = ChangeCalculator.pennies(totalCost, cash);
				} catch (InvalidValuesException e) {
					change = e.getMessage();
				}

				MyDialogFragment myDialogFragment = new MyDialogFragment();
		        Bundle args = new Bundle();
		        args.putString(HAS_CASH_KEY, change);
		        myDialogFragment.setArguments(args);
		        myDialogFragment.show(getFragmentManager(), "tag");
		        
				history.add(new Item(cash, totalCost, change));

			}
		} else if (addCost == view) {
			// gets the cost and checks to see if it is empty
			// if it is empty it tells the user with a dialog box

			if (textCost.equals("") || textCost.equals("0")) {
				
				MyDialogFragment myDialogFragment = new MyDialogFragment();
		        Bundle args = new Bundle();
		        args.putString(CASH_IS_EMPTY_KEY, "You have entered no money!");
		        myDialogFragment.setArguments(args);
		        myDialogFragment.show(getFragmentManager(), "tag");
		        
			} if (new BigDecimal(textCost).scale()>2 ){
				MyDialogFragment myDialogFragment = new MyDialogFragment();
		        Bundle args = new Bundle();
		        args.putString(TOO_MANY_DECIMAL_PLACES_KEY, "You have entered too many decimal places!");
		        myDialogFragment.setArguments(args);
		        myDialogFragment.show(getFragmentManager(), "tag");
		        
			}
			else {
				// if it is not empty it adds the value to the total cost and
				// resets the edit text
				BigDecimal cost = new BigDecimal(textCost);

				totalCost = totalCost.add(cost);
				enterCost.setText("");

				textTotalCost.setText(totalCost.toString());
			}
		}
	}
	
	public void resetText (){
		enterCost.setText("");
		enterFunds.setText("");
		textTotalCost.setText("0");
		totalCost = BigDecimal.ZERO;
	}

}
